<?php

require ('config.php');

function  laSo($a)
{
    if(is_numeric($a) )
        return true;
    else
        return false;
    //$dem = 0;   
    //for ($i = 0; $i < strlen($a); $i++)
    //{
//        if ((( $a[$i] <'0' || $a[$i] > '9') && $a[$i] != '.')|| $dem > 1)
//            return false;
//        if ($a[$i] = '.')
//            $dem ++;
//        
//    }
//    return true;
}



// LAY SAN BAY
function CSDL_SanBay(&$sanBay, &$sanBay_Ten)
{
    $sanBay = array();
    $sanBay_Ten = array();
     
    $query_SanBay = "SELECT * FROM sanbay";
    $result_SanBay = @mysql_query($query_SanBay);
    $i = 1;
    $sanBay[0] = 0;
    while( $row_SanBay = mysql_fetch_array($result_SanBay, MYSQL_ASSOC))
    {
        $sanBay[$i] = $row_SanBay['MASB']; 
        $sanBay_Ten[$row_SanBay['MASB']] = $row_SanBay['TENSB'];  
        $sanBay[0] += 1; 
        $i++;
    }
}

//
CSDL_SanBay($sanBay, $sanBay_Ten);
//echo '<br>'.'Co tong cong :'.$sanBay[0].' San Bay'.'<br>';
//
//for($d = 1; $d <= $sanBay[0]; $d++)
//{
//    echo $sanBay[$d].'    ==>   '.$sanBay_Ten[$sanBay[$d]].'<br>';
//}
//

// LAY HANG VE
function CSDL_hangVe(&$hangVe, &$hangVe_Gia)
{
    $hangVe = array();
    $hangVe_Gia = array();
     
    $query_HangVe = "SELECT * FROM hangve";
    $result_HangVe = @mysql_query($query_HangVe);
    $i = 1;
    $hangVe[0] = 0;
    while( $row_HangVe = mysql_fetch_array($result_HangVe, MYSQL_ASSOC))
    {
        $hangVe[$i] = $row_HangVe['HANGVE']; 
        $hangVe_Gia[$row_HangVe['HANGVE']] = $row_HangVe['GIA'];  
        $hangVe[0] += 1; 
        $i++;
    }
}
//
CSDL_hangVe($hangVe, $hangVe_Gia);
//echo '<br>'.'Co tong cong :'.$hangVe[0].' Hang Ve'.'<br>';
//
//for($d = 1; $d <= $hangVe[0]; $d++)
//{
//    echo $hangVe[$d].'    ==>   '.$hangVe_Gia[$hangVe[$d]].'<br>';
//}
//

// LAY QUY DINH

function CSDL_quyDinh(&$quyDinh)
{
    
    $quyDinh = array();


    $query_QuyDinh = "SELECT * FROM quydinh";
    $result_QuyDinh = @mysql_query($query_QuyDinh);
    while( $row_QuyDinh = mysql_fetch_array($result_QuyDinh, MYSQL_ASSOC))
    {
        
        $quyDinh['SLSBAY'] = $row_QuyDinh['SLSBAY'];
        $quyDinh['TGBAYMIN'] = $row_QuyDinh['TGBAYMIN'];
        $quyDinh['SBTGMAX'] = $row_QuyDinh['SBTGMAX'];
        $quyDinh['TGDUNGMIN'] = $row_QuyDinh['TGDUNGMIN'];
        $quyDinh['TGDUNGMAX'] = $row_QuyDinh['TGDUNGMAX'];
        $quyDinh['SLHANGVE'] = $row_QuyDinh['SLHANGVE'];
        $quyDinh['DATVECHAM'] = $row_QuyDinh['DATVECHAM'];
        $quyDinh['HUYVECHAM'] = $row_QuyDinh['HUYVECHAM'];
        
    }
}//
CSDL_quyDinh($quyDinh);
//echo '<br><br>'."CAC THONG SO CUA QUY DINH".'<br>';
//   echo $quyDinh['SLSBAY'].'<br>';//['SLSBAY'];
//  echo $quyDinh['TGBAYMIN'].'<br>';//['TGBAYMIN'];
//   echo $quyDinh['SBTGMAX'].'<br>';//['TGBAYMAX'];
//   echo $quyDinh['TGDUNGMIN'].'<br>';//['TGDUNGMIN'];
//   echo $quyDinh['TGDUNGMAX'].'<br>';//['TGDUNGMAX'];
//   echo $quyDinh['SLHANGVE'].'<br>';//['SLHANGVE'];
//   echo $quyDinh['DATVECHAM'].'<br>';//['DATVECHAM'];
//   echo $quyDinh['HUYVECHAM'].'<br>';//['HUYVECHAM'];
//
//
// LAY SAN BAY TRUNG GIAN
function CSDL_sanBayTrungGian(&$sanBayTrungGian, &$sanBayTrungGian_MACB )
{
    
    $sanBayTrungGian = array();
    $sanBayTrungGian_MACB = array();
     
    $query_SanBayTrungGian = "SELECT * FROM sanbaytrunggian";
    $result_SanBayTrungGian = @mysql_query($query_SanBayTrungGian);
    $i = 1;
    $sanBayTrungGian[0] = 0;
    while( $row_SanBayTrungGian = mysql_fetch_array($result_SanBayTrungGian, MYSQL_ASSOC))
    {
        $sanBayTrungGian[$i] = $row_SanBayTrungGian['MASB']; 
        $sanBayTrungGian_MACB[$row_SanBayTrungGian['MASB']] = $row_SanBayTrungGian['MACB'];  
        $sanBayTrungGian[0] += 1; 
        $i++;
    }
    
}
//
CSDL_sanBayTrungGian($sanBayTrungGian, $sanBayTrungGian_MACB);
//echo '<br>'.'Co tong cong '.$sanBayTrungGian[0].' San bay trung gian'.'<br>';
//
//for($d = 1; $d <= $sanBayTrungGian[0]; $d++)
//{
//    echo $sanBayTrungGian[$d].'    ==>   '.$sanBayTrungGian_MACB[$sanBayTrungGian[$d]].'<br>';
//}
//
//// Lay so luong ghe
//
function CSDL_soLuongGhe(&$soLuongGhe)
{
        
    $soLuongGhe = array();
    
    $query_SoLuongGhe = "SELECT * FROM soluongghe ORDER BY MACB";
    $result_SoLuongGhe = @mysql_query($query_SoLuongGhe);
    $i = 1;
    $soLuongGhe[0] = 0;
    while( $row_SoLuongGhe = mysql_fetch_array($result_SoLuongGhe, MYSQL_ASSOC))
    {
        $soLuongGhe[$i][MACB] = $row_SoLuongGhe['MACB']; 
        $soLuongGhe[$i][HANGVE] = $row_SoLuongGhe['HANGVE']; 
        $soLuongGhe[$i][SOLUONG] = $row_SoLuongGhe['SOLUONG']; 
          
        $soLuongGhe[0] += 1; 
        $i++;
    }

    
}
//
CSDL_soLuongGhe($soLuongGhe);
//echo '<br>'.'Ket Qua So Luong Ghe '.$soLuongGhe[0].'<br>';
//
//for($d = 1; $d <= $soLuongGhe[0]; $d++)
//{
//    echo $soLuongGhe[$d][MACB].'    ==>   '.$soLuongGhe[$d][HANGVE].'    ==>   '.$soLuongGhe[$d][SOLUONG].'<br>';
//}
//
//
////THONG TIN CHUYEN BAY
//
function CSDL_chuyenBay(&$chuyenBay)
{
        
    $chuyenBay = array();
    
    $query_ChuyenBay = "SELECT * FROM chuyenBay ORDER BY NGAYGIODI DESC";
    $result_ChuyenBay = @mysql_query($query_ChuyenBay);
    $i = 1;
    $chuyenBay[0] = 0;
    while( $row_ChuyenBay = mysql_fetch_array($result_ChuyenBay, MYSQL_ASSOC))
    {
        $chuyenBay[$i][MACB] = $row_ChuyenBay['MACB']; 
        $chuyenBay[$i][SBDI] = $row_ChuyenBay['SBDI']; 
        $chuyenBay[$i][SBDEN] = $row_ChuyenBay['SBDEN']; 
        $chuyenBay[$i][NGAYGIODI] = $row_ChuyenBay['NGAYGIODI']; 
        $chuyenBay[$i][THOIGIANBAY] = $row_ChuyenBay['THOIGIANBAY']; 
        $chuyenBay[$i][THOIGIANDUNG] = $row_ChuyenBay['THOIGIANDUNG'];
          
        $chuyenBay[0] += 1; 
        $i++;
    }
    
}
//
CSDL_chuyenBay($chuyenBay);
//
//echo '<br>'.'Ket Qua CHUYEN BAY '.$chuyenBay[0].'<br>';
//
//for($i = 1; $i <= $chuyenBay[0]; $i++)
//{
//    ECHO $chuyenBay[$i][MACB].'==>'.//['MACB']; 
//    $chuyenBay[$i][SBDI].'==>'.//['SBDI']; 
//    $chuyenBay[$i][SBDEN].'==>'.//['SBDEN']; 
//    $chuyenBay[$i][NGAYGIODI].'==>'.//['NGAYGIODI']; 
//    $chuyenBay[$i][THOIGIANBAY].'==>'.//['THOIGIANBAY']; 
//    $chuyenBay[$i][THOIGIANDUNG].'<br>';//['THOIGIANDUNG'];
//}

//LAY SO GHE DAT

function laySoGheDat($MACB)
{
    $query_SoGheDat = "SELECT COUNT(STT) as soluong FROM ve WHERE MACB = '$MACB'";
    $result_SoGheDat = @mysql_query($query_SoGheDat);
    $row = mysql_fetch_array($result_SoGheDat, MYSQL_ASSOC);
    return $row['soluong'];
}

function laySoGheDatHV($MACB, $HV)
{
    $query_SoGheDat = "SELECT COUNT(STT) as soluong FROM ve WHERE MACB = '$MACB'AND HANGVE ='$HV' ";
    $result_SoGheDat = @mysql_query($query_SoGheDat);
    $row = mysql_fetch_array($result_SoGheDat, MYSQL_ASSOC);
    return $row['soluong'];
}

function laySoGheTrong($MACB)
{
    $ketqua = 0;
    CSDL_soLuongGhe($soLuongGhe);
    for($d = 1; $d <= $soLuongGhe[0]; $d++)
    {
        
        if($soLuongGhe[$d][MACB] == $MACB)
        {
            
            $ketqua += $soLuongGhe[$d][SOLUONG];
        }
    }
    return $ketqua - laySoGheDat($MACB);
}

function laySoGheTrongHV($MACB, $HV)
{
    $ketqua = 0;
    
    $query_SoLuongGhe = "SELECT SOLUONG FROM soluongghe WHERE HANGVE = '$HV' AND MACB = '$MACB' ";
    $result_SoLuongGhe = @mysql_query($query_SoLuongGhe);
    $row_SoLuongGhe = mysql_fetch_array($result_SoLuongGhe, MYSQL_ASSOC);
    $ketqua = $row_SoLuongGhe['SOLUONG'];

    return $ketqua - laySoGheDatHV($MACB,$HV);
}


function layDoanhThu($MACB)
{
    $doanhthu = 0;
    $query = "SELECT SUM(GIA) as doanhthu FROM ve WHERE MACB = '$MACB' AND TINHTRANG = 'Xác Nhận' ";
    $result = @mysql_query($query);
    $row =  mysql_fetch_array($result, MYSQL_ASSOC);
    if($row['doanhthu'])
        $doanhthu = $row['doanhthu'];
    return $doanhthu;
}

// KIEM TRA HANG VE - INPUT MACB, HANGVE -> OUTPUT CON VE OR HET VE

function ktHangVe($MACB, $HANGVE)
{
    $tongSoGheDat = 0;
    $query = "SELECT COUNT(STT) as DEM FROM ve WHERE MACB = '$MACB' AND HANGVE = '$HANGVE'";
    $result = @mysql_query($query);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $tongSoGheDat = $row['DEM'];
    
    $query = "SELECT SOLUONG FROM soluongghe WHERE MACB = '$MACB' AND HANGVE = '$HANGVE'";
    $result = @mysql_query($query);
    $row =  mysql_fetch_array($result, MYSQL_ASSOC);
    
    $tongSoGhe = $row['SOLUONG'];

    
    if($tongSoGhe > $tongSoGheDat)
    {
        return true;
    }

    return false; 
}


?>